<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
</head>
<body>
    <div id="studentName"></div>
    <div id="studentNo"></div>
    <div id="gender"></div>
    <div id="verifyCode"></div>
    <div id="email"></div>
</body>

<script type="text/javascript">
    $(document).ready(function () {
        var token = localStorage.getItem("token")
        var username = localStorage.getItem("username")

        $.ajax({
            type: 'post',
            url: 'http://localhost:8090/user/info',
            contentType: 'application/json',
            dataType: 'json',
            data: JSON.stringify({
                "username": username,
                "token": token
            }),
            //跨域
            xhrFields: {
                withCredentials: true
            },

            function:setCookie("access_token",token,1),

            success: function (data){
                console.log(data)
                if(data.code == 200){
                    $("#name").text("姓名：" + data.result.studentName)
                    $("#number").text("学号：" + data.result.studentNo)
                    if(data.result.gender === "0"){
                        $("#gender").text("性别：男")
                    }else{
                        $("#gender").text("性别：女")
                    }

                    $("#code").text("认证码：" + data.result.verifyCode)
                    $("#email").text("邮箱：" + data.result.email)

                }else{
                    $("#userInfo").text(data.message)
                }
            },
            error: function (){
                alert('参数错误')
            }
        })
    })

    // 设置cookie
    export function setCookie(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie =
            c_name +
            '=' +
            encodeURIComponent(value) +
            ';expires=' +
            exdate.toGMTString() +
            ';path=/';
    }
    // 读取cookie
    export function getCookie(c_name) {
        let c_start = null;
        let c_end = null;
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + '=');
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(';', c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return decodeURIComponent(document.cookie.substring(c_start, c_end));
            }
        }
        return '';
    }
    // 检查cookie
    export function checkCookie(c_name) {
        let username = getCookie(c_name);
        console.log(username);
        if (username != null && username != '') {
            return true;
        } else {
            return false;
        }
    }
    // 清除cookie
    export function clearCookie(name) {
        setCookie(name, '', -1);
    }


</script>
</html>